home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Documentation / DirectX9 / directx9_m.chm / directx / code / contextmenu.htc < prev    next >
Encoding:
Text File  |  2004-09-30  |  8.1 KB  |  265 lines

  1. <!--
  2. Author: stcote
  3. Created: 3-18-99
  4. Modified: 4-8-99
  5.  
  6. Bugs: Timeout needed on sub-menus.
  7.  
  8. -->
  9. <PUBLIC:Component>
  10. <METHOD NAME="openContextMenu" />
  11. <METHOD NAME="createMenuItem" />
  12. <METHOD NAME="createContextMenu" />
  13. <METHOD NAME="createMenu" / >
  14. <EVENT ID="onBehaviorReady" NAME="onbehaviorready" />
  15. <EVENT ID="onContextOpen" NAME="oncontextopen" />
  16. <ATTACH event="ondocumentready" handler="fnDocReady"/>
  17. </PUBLIC:Component>
  18. <SCRIPT>
  19. var oMenu;
  20. var oStyleInfo;
  21. var oObject=null;
  22. var aSubs=new Array();
  23. var aMenus=new Array();
  24.  
  25. function fnDocReady(){
  26.     element.oncontextmenu=openContextMenu;
  27.     fnCreateStyleSheets();
  28.     fnFireOnBehaviorReady("type","behavior","behaviorStatus","looks good");
  29. }
  30.  
  31. function fnStub(){
  32. }
  33.  
  34. function closeContextMenu(){
  35.     oObject=null;
  36.     window.document.releaseCapture();
  37.     for(var i=0;i<aSubs.length;i++){
  38.         aSubs[i].menuState=false;
  39.         aSubs[i].style.display="none";
  40.         if(typeof(aSubs[i]).menuItem=="object"){
  41.             aSubs[i].menuItem.className="menuitem";
  42.         }
  43.     }
  44. }
  45. function openContextMenu(){
  46.     if(typeof(aMenus[event.srcElement.nodeName])=="object"){
  47.         oMenu=aMenus[event.srcElement.nodeName];
  48.         if((oMenu.dependency==false)||(event.srcElement.getAttribute(oMenu.dependency))){
  49.             if((oMenu.menuState==true)&&(event.type!="contextmenu")){
  50.                 oObject=null;
  51.                 window.document.releaseCapture();
  52.                 for(var i=0;i<aSubs.length;i++){
  53.                     aSubs[i].menuState=false;
  54.                     aSubs[i].style.display="none";
  55.                     if(typeof(aSubs[i]).menuItem=="object"){
  56.                         aSubs[i].menuItem.className="menuitem";
  57.                     }
  58.                 }
  59.             }
  60.             else{
  61.                 fnFireOnContextOpen("type","oncontextopen");
  62.                 oObject=event.srcElement;
  63.                 oMenu.setCapture();
  64.                 oMenu.onlosecapture=closeContextMenu;
  65.                 oMenu.menuState=true;
  66.                 oMenu.style.display="block";                
  67.                 var iClientY=event.y;
  68.                 var iClientX=event.x;                
  69.                 var iScrollTop=window.document.body.scrollTop;
  70.                 var iScrollLeft=window.document.body.scrollLeft;
  71.                 var iBodyWidth=window.document.body.offsetWidth;
  72.                 var iBodyHeight=window.document.body.offsetHeight;
  73. //                alert((oMenu.offsetHeight + iClientY + iScrollTop) + "\n" + (iBodyHeight + iScrollTop));
  74.                 var iTop=0
  75.                 var iLeft=0;
  76.                 if((oMenu.offsetWidth + iClientX) < (iBodyWidth + iScrollLeft)){
  77.                     iLeft=event.clientX + iScrollLeft;
  78.                 }
  79.                 else{
  80.                     iLeft=iScrollLeft + event.clientX - oMenu.offsetWidth;
  81.                 }
  82.                 if((oMenu.offsetHeight + iClientY + iScrollTop) < (iBodyHeight + iScrollTop)){
  83.                     iTop=event.clientY + iScrollTop;
  84.                 }
  85.                 else{
  86.                     iTop=iScrollTop + event.clientY - oMenu.offsetHeight;
  87.                 }
  88.                 oMenu.style.top=iTop;
  89.                 oMenu.style.left=iLeft;
  90.                                 
  91.             }
  92.             event.returnValue=false;
  93.         }
  94.         else{
  95.             if(oMenu.menuState==true){
  96.                 closeContextMenu();
  97.             }
  98.             event.returnValue=true;
  99.         }
  100.     }
  101.     else{
  102.         oObject=null;
  103.         window.document.releaseCapture();
  104.         for(var i=0;i<aSubs.length;i++){
  105.             aSubs[i].menuState=false;
  106.             aSubs[i].style.display="none";
  107.             if(typeof(aSubs[i]).menuItem=="object"){
  108.                 aSubs[i].menuItem.className="menuitem";
  109.             }
  110.         }
  111.  
  112.     }
  113. }
  114. function fnCreateStyleSheets(){
  115.     if (!element.document.body.contextMenu){
  116.         element.document.body.contextMenu=window.document.createStyleSheet();
  117.     }
  118.     oStyleInfo=element.document.body.contextMenu;
  119.     oStyleInfo.addRule(".menu","position: absolute; display: none; background-color: #CFCFCF; width: 150; overflow-x: hidden; overflow-y: auto; border: '2 outset'; z-index: 2;");
  120.     oStyleInfo.addRule(".menuitem","padding-left: 10px; padding-right: 6; cursor: hand; background-color: #CFCFCF; font-size: 8pt; font-family: Arial; width: 150; color: #000000;");
  121.     oStyleInfo.addRule(".menuitem2","padding-left: 10px; padding-right: 6;  cursor: hand; background-color: #0000FF; color: #FFFFFF; font-size: 8pt; font-family: Arial; width: 150;");
  122.     
  123. }
  124. function createMenu(sElementName,sDependent){
  125.     var oNewMenu=createContextMenu(true);
  126.     if(arguments[1]){
  127.         oNewMenu.dependency=sDependent;
  128.     }
  129.     else{
  130.         oNewMenu.dependency=false;
  131.     }
  132.     aMenus[sElementName]=oNewMenu;
  133.     return oNewMenu;
  134. }
  135. function createContextMenu(){
  136.     oContextMenu=document.createElement("SPAN");
  137.     oContextMenu.menuState=false;
  138.     window.document.body.appendChild(oContextMenu);
  139.     if(arguments[0]){
  140.         oContextMenu.onclick=fnMenuClick;
  141.         oContextMenu.onmouseover=fnFlash;
  142.         oContextMenu.onmouseout=fnFlash;
  143.     }
  144.     aSubs[aSubs.length]=oContextMenu;
  145.     oContextMenu.className="menu";
  146.     return oContextMenu;
  147. }
  148. function fnMenuClick(){
  149.     var oWorkItem=event.srcElement;
  150.     if(oWorkItem.menu_action){
  151.         if(oObject!=null){    
  152.             oWorkItem.menu_action();
  153.             if(oWorkItem.hasSub==false){
  154.                 oWorkItem.className="menuitem";
  155.             }
  156.         }
  157.     }
  158.     if((oMenu.menuState==true)&&(!oWorkItem.hasSub)){
  159.         openContextMenu();
  160.     }
  161. }
  162. //<SPAN item_status="dull" menu_action="fnRedir(0)" ID=oCopy CLASS="menuitem">DHTML References</span>
  163. function createMenuItem(sTitle,fpAction,oContextMenu){
  164.     var bSub=(arguments[3] ? true : false);
  165.     var oItem=document.createElement("SPAN");
  166.     var oTitle=document.createTextNode(sTitle);
  167.     oItem.appendChild(oTitle);
  168.     oItem.item_status=false;
  169.     oItem.menu_action=fpAction;
  170.     oItem.className="menuitem";
  171.     oItem.hasSub=false;
  172.     oContextMenu.appendChild(oItem);    
  173.     if(bSub==true){
  174.         oItem.hasSub=true;
  175.         oItem.subState=false;
  176.         oItem.style.backgroundImage="url(/workshop/graphics/subMenu.gif)";
  177.         oItem.style.backgroundRepeat="no-repeat";
  178.         oItem.style.setExpression("backgroundPosition",oContextMenu.uniqueID + ".offsetWidth - 15");
  179.         var oSubMenu=createContextMenu();
  180.         oItem.subMenu=oSubMenu;
  181.         oSubMenu.menuItem=oItem;
  182.     }
  183.     return oItem;
  184. }
  185. function fnFlash(){
  186.     var oWorkItem=event.srcElement;
  187.     var sType=event.type;
  188.     // If this is a menu item:
  189.     if(oWorkItem.menu_action){
  190.         // If this is the mouseover event:
  191.         if(sType=="mouseover"){
  192.             // Set the menu item to a 'flash' state
  193.             oWorkItem.className="menuitem2";
  194.             // Set a boolean to indicate the state.
  195.             oWorkItem.item_status="flash";
  196.             // If this menu item has a sub menu:
  197.             if(oWorkItem.hasSub==true){
  198.                 // Retrieve the object reference to the sub menu
  199.                 var oSub=oWorkItem.subMenu;
  200.                 // Retrieve the object reference to the menu item parent
  201.                 var oParent=oWorkItem.parentNode;
  202.                 // Set a boolean on the menu item and sub menu to indicate the sub menu is open.
  203.                 oWorkItem.subState=true;
  204.                 oSub.menuState=true;
  205.                 // Move the sub menu to an offset of the menu item
  206.                 var iOffsetLeft=oParent.offsetLeft + oParent.offsetWidth - 5;
  207.                 var iOffsetTop=oParent.offsetTop;
  208.                 var iScrollTop=window.document.body.scrollTop;
  209.                 var iScrollLeft=window.document.body.scrollLeft;
  210.                 var iBodyWidth=window.document.body.offsetWidth;
  211.                 var iBodyHeight=window.document.body.offsetHeight;
  212.                 
  213.                 var iTop=0
  214.                 var iLeft=0;
  215.                 oSub.style.display="block";                
  216. //                alert((oSub.offsetWidth) + "\n" + (iBodyWidth + iScrollLeft));
  217. //                if((oSub.offsetWidth + iOffsetLeft + iScrollLeft) < (iBodyWidth + iScrollLeft)){
  218.                     iLeft=iOffsetLeft - 5;
  219. //                }
  220. //                else{
  221. //                    iLeft=iOffsetLeft - oSub.offsetWidth - 5;
  222. //                }
  223. //                if((oSub.offsetHeight + iOffsetTop + iScrollTop) < (iBodyHeight + iScrollTop)){
  224.                     iTop=iOffsetTop;
  225. //                }
  226. //                else{
  227. //                    iTop=iOffsetTop;
  228. //                }
  229.                 
  230.                 oSub.style.top=iTop;
  231.                 oSub.style.left=iLeft;                
  232.  
  233.                 // Display the sub menu.
  234.             }
  235.         }
  236.         if(sType=="mouseout"){
  237.             // If the menu item has an open sub menu, close it.
  238.             if((oWorkItem.hasSub==true)&&(oWorkItem.subState==true)){
  239.                 var oSub=oWorkItem.subMenu;
  240.                 // Only close the sub menu if the mouse is not moving to the sub menu.
  241.                 if(event.toElement!=oSub){
  242.                     oSub.style.display="none";
  243.                     oSub.menuState=false;
  244.                     oWorkItem.subState=false;
  245.                     oWorkItem.className="menuitem";
  246.                 }
  247.             }
  248.             else{
  249.                 oWorkItem.className="menuitem";
  250.             }
  251.         }
  252.     }
  253. }
  254. function fnFireOnContextOpen(sName, sValue){
  255.     var oEvent=createEventObject();
  256.     oEvent.setAttribute(sName,sValue);
  257.     onContextOpen.fire(oEvent);
  258. }
  259. function fnFireOnBehaviorReady(sName, sValue, sEnv, sEnvVal){
  260.     var oEvent=createEventObject();
  261.     oEvent.setAttribute(sName,sValue);
  262.     oEvent.setAttribute(sEnv,sEnvVal);
  263.     onBehaviorReady.fire(oEvent);
  264. }
  265. </SCRIPT>